<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>借货出库记录</title>
    <script type="text/javascript">
      var newLoanDeliveryWin;
      var editLoanDeliveryWin;
      var loanDeliveryId;
      var editLoanDeliveryFromLoanWin;
      Ext.onReady(function(){

        var loanNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '借货单号',
          name: 'loan_number',
          anchor: '100%'
        });

        var orderNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '借货出库单号',
          name: 'order_number',
          anchor: '100%'
        });

        var customer = Ext.create('Ext.form.ComboBox', {
          fieldLabel: '客户',
          store: customerStore,
          valueField: 'id',
          displayField: 'name',
          name: 'customer_id',
          loadingText: '搜索中...',
          pageSize: 10,          
          minChars: 0,
          anchor: '100%',
          listConfig: {
            getInnerTpl: function() {
              return '<div data-qtip="{number}">{name}</div>';
            }
          }
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '借货出库日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var submitButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            loanDeliveryDS.removeAll();
            loanDeliveryDS.getProxy().extraParams = queryFormPanel.getForm().getFieldValues();
            toolBar.moveFirst();  
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 80,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1000},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [loanNumber]
                    }, {
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [orderNumber]
                    }, {
                      columnWidth: .35,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [customer]
                    }]
                },{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [beginDate]
                    }, {
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [endDate]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //显示借货出库操作时的窗口介面。
        //----------------------------------------------------------------------
        //借货出库Grid Store
        var loanDeliveryDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'LoanDeliveryModel',
          proxy : {
            type : 'ajax',
            url : '/loan_delivery/getAllLoanDelivery',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : pageSize,
          autoLoad : true
        });

        var toolBar = Ext.create('Ext.toolbar.Paging', {
          pageSize: pageSize,
          store: loanDeliveryDS,
          displayInfo: true,
          displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
          emptyMsg: "没有记录",
          items:["-",{
              text: '新增借货出库单',
              iconCls: 'addItem',
              disabled:true,
              handler : function(){
                newLoanDeliveryWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width: 810,
                  height: 500,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加借货出库单',
                  autoLoad: {
                    url: "/loan_delivery/newLoanDelivery",
                    scripts: true
                  }
                });
                newLoanDeliveryWin.on("destroy",function(){
                  loanDeliveryDS.load();
                });
                newLoanDeliveryWin.resizable = false;
                newLoanDeliveryWin.show();
              }
            },'-',{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = loanDeliveryGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  loanDeliveryId = records[0].get('id');
                  var currentLoanNumber = records[0].get('loan_number');
                  var dealWithStatus = records[0].get('deal_with_status');
                  if(records[0].get('auditing_status') == "" || records[0].get('auditing_status').indexOf('驳回新增') > -1 || records[0].get('auditing_status').indexOf('驳回撤消') > -1){
                    editLoanDeliveryFromLoanWin = Ext.create('Ext.window.Window', {
                      layout: 'fit',
                      width: 917,
                      height: 510,
                      closeAction: 'destroy',
                      plain: true,
                      modal: true,
                      constrain:true,
                      //modal: true,
                      title: "编辑借货订单:'"+currentLoanNumber+"'的借货出库单",
                      autoLoad: {
                        url: "/loan_delivery/editDeliveryFromLoan?id="+loanDeliveryId,
                        scripts: true
                      }
                    });
                    editLoanDeliveryFromLoanWin.on("destroy",function(){
                      loanDeliveryDS.load();
                    });
                    editLoanDeliveryFromLoanWin.resizable = false;
                    editLoanDeliveryFromLoanWin.show();
                  }
                }   
              }
            },'-',{
              text: '撤消',
              iconCls: 'cancel',
              handler : function(){
                var records = loanDeliveryGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消'){
                    var orderNumber = records[0].get('order_number')
                    loanDeliveryId = records[0].get('id');
                    Ext.MessageBox.confirm("提示","你确认要申请撤消订单号为： \""+orderNumber+"\" 的借货出库单吗？",function(btn){
                      if(btn=="yes"){
                        Ext.Ajax.request({
                          url : '/loan_delivery/applyToCancel?id=' + loanDeliveryId,
                          success : function (result, request) {
                            loanDeliveryDS.load();
                          },
                          failure : function (result, request){
                            Ext.MessageBox.show({
                              title: '消息',
                              msg: "通讯失败，请从新操作",
                              buttons: Ext.MessageBox.OK,
                              icon: Ext.MessageBox.WARNING
                            });
                          },
                          method : 'GET'
                        });
                      }else{
                        //alert("no");
                      }
                    });
                  }
                }
              }
            },'-',{
              text: '打印借货出库凭据',
              iconCls: 'print',
              handler : function(){
                var records = loanDeliveryGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消'){
                    this.disable();
                    loanDeliveryId = records[0].get('id');
                    //LODOP.SET_PRINT_PAGESIZE(1,0,0,'A5')
                    LODOP.PRINT_INIT("");
                    LODOP.SET_PRINT_PAGESIZE(1,"210mm","148mm","")
                    //LODOP.SET_PRINT_PAGESIZE(1,"148mm","210mm","")
                    LODOP.ADD_PRINT_URL(30,'5%',"90%","100%",'http://'+window.location.host+'/loan_delivery/loan_delivery_print_header?id='+loanDeliveryId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    LODOP.ADD_PRINT_TBURL(170,'5%',"90%","43%",'http://'+window.location.host+'/loan_delivery/loan_delivery_print_content?id='+loanDeliveryId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",4);
                    LODOP.ADD_PRINT_URL(425,'5%',"90%","100%",'http://'+window.location.host+'/loan_delivery/loan_delivery_print_footer?id='+loanDeliveryId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    LODOP.ADD_PRINT_TEXT(495,'15%',"80%","8%","第一联：成本（白） 第二联：财务（绿） 第三联：提货（粉） 第四联：客户（黄） 第五联：销售（兰）");
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    LODOP.ADD_PRINT_TEXT(515,'35%',"90%","8%","页号：第#页/共&页 打印日期："+getDate());
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",2);
                    LODOP.PREVIEW();
                    this.enable();
                  }
                }
              }
            }]
        })
        
        //生成要订单的产品的Grid
        var loanDeliveryGrid = Ext.create('Ext.grid.Panel', {
          title:'借货出库单列表',
          store: loanDeliveryDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '借货订单单号',
              dataIndex: 'loan_number',
              width: 170
            },{
              header: '借货订单状态',
              dataIndex: 'loan_status',
              width: 85,
              renderer: auditingStatusRender
            },{
              header: '借货出库单单号',
              dataIndex: 'order_number',
              width: 110
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '处理状态',
              dataIndex: 'deal_with_status',
              width: 70
            },{
              header: '调出仓库',dataIndex: 'warehouse_name',width: 100
            },{
              header: '到客户',
              dataIndex: 'customer_name',
              width: 190
            },{
              header: '要求借货出库日期',
              dataIndex: 'loan_action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 110
            },{
              header: '实际借货出库日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 110
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 70
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          //renderTo: 'loan_delivery_list_grid',
          width: screenWidth-165,
          height: 315,
          frame: true,
          tbar: toolBar
        });

        loanDeliveryGrid.on('itemdblclick', function(gridPanel, record,item,index,e,options){
          loanDeliveryId = loanDeliveryDS.getAt(index).get('id');
          var loanDeliveryNumber = loanDeliveryDS.getAt(index).get('order_number');
          //loanDeliveryDS.load();
          loanDeliveryDetailDS.removeAll();
          loanDeliveryDetailDS.load({params:{'loan_delivery_id': loanDeliveryId}});
          loanDeliveryDetailGrid.setTitle("借货出库单："+loanDeliveryNumber+"，的详细信息");
        });

        //右键菜单，以弹出此订单相关的入库单
        //右键
        loanDeliveryGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          loanDeliveryId = loanDeliveryDS.getAt(index).get('id');
          currentOrderNumber = loanDeliveryDS.getAt(index).get('order_number');
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        var loanDeliveryHxItem = Ext.create('Ext.menu.Item', {
          text: '【变更历史】',
          //iconCls: 'album',
          handler: rightMenuLoanDeliveryHxFn
        });

        function rightMenuLoanDeliveryHxFn(){
          loanDeliveryHxWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 960,
            height: 550,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '借货出库单：\"'+currentOrderNumber+'\"的变更记录',
            autoLoad: {
              url: "/loan_delivery/hxListForLoanDelivery?id="+loanDeliveryId,
              scripts: true
            }
          });
          loanDeliveryHxWin.on("destroy",function(){
            loanDeliveryDS.load();
          });
          loanDeliveryHxWin.resizable = false;
          loanDeliveryHxWin.show();
        }
        
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [loanDeliveryHxItem]
        });
        
        //--------------------------借货订单详细，双击后可显示数据----------------
        //借货订单Grid Store Detail
        var loanDeliveryDetailDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'LoanDeliveryDetailModel',
          proxy : {
            type : 'ajax',
            url : '/loan_delivery/getLoanDeliveryDetailsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });

        //生成要订单的产品的Grid
        var loanDeliveryDetailGrid = Ext.create('Ext.grid.Panel', {
          title:'借货出库单详细列表',
          store: loanDeliveryDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 260
            }, {
              header: '产品编号',
              dataIndex: 'product_number',
              width:95
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 60
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }],
          loadMask: true,
          width: screenWidth-165,
          height: dgHeight,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: totalHeight,autoScroll:true,
          frame:false,
          
          layout: {type: 'table',columns:1},
          
          items:[queryFormPanel,loanDeliveryGrid,loanDeliveryDetailGrid]
        });
        workbenchPanel.render("loan_delivery_list");
        
      })
    </script>
  </head>
  <body>
    <div id="loan_delivery_list"></div>
  </body>
</html>
